﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SignalShopBase;

namespace TestModule
{
    public class FilterModule : SignalShopModule
    {
        public FilterModule() : base(NAME, PATH) { }

        public override void Execute(Signal target, Signal source)
        {
            FilterDialog fd = new FilterDialog();

            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int N = target.N;

                int fMin = (int)fd.MinFrequency;
                int fMax = (int)fd.MaxFrequency;

                switch (fd.FilterType)
                {
                    case FilterType.LowPass:
                        for (int n = -N; n <= N; n++)
                        {
                            if (Math.Abs(n) <= fMax) 
                                target[n].Set(1, 0); 
                            else target[n].Set(0, 0);
                        }
                        break;
                    case FilterType.HighPass:
                        for (int n = -N; n <= N; n++)
                        {
                            if (Math.Abs(n) >= fMin) 
                                target[n].Set(1, 0); 
                            else target[n].Set(0, 0);
                        }
                        break;
                    case FilterType.BandPass:
                        for (int n = -N; n <= N; n++)
                        {
                            if ((Math.Abs(n) >= fMin) && (Math.Abs(n) <= fMax)) 
                                target[n].Set(1, 0); 
                            else target[n].Set(0, 0);
                        }
                        break;
                    case FilterType.BandStop:
                        for (int n = -N; n <= N; n++)
                        {
                            if ((Math.Abs(n) >= fMin) && (Math.Abs(n) <= fMax))
                                target[n].Set(0, 0);
                            else target[n].Set(1, 0);
                        }
                        break;
                }
            }
        }

        private const string NAME = "Filter";
        private const string PATH = "Generate/Filter";
    }
}
